package org.example;

import java.util.ArrayList;
import java.util.List;
//单例模式
//空调控制器单例类
public class AirConditionerController {
    private static AirConditionerController instance;
    private List<AirConditioner> airConditioners;

    private AirConditionerController() {
        airConditioners = new ArrayList<>();
    }

    public static AirConditionerController getInstance() {
        if (instance == null) {
            instance = new AirConditionerController();
        }
        return instance;
    }

    // 添加或删除订阅的空调
    public void addAirConditioner(AirConditioner airConditioner) {
        airConditioners.add(airConditioner);
    }

    public void removeAirConditioner(AirConditioner airConditioner) {
        airConditioners.remove(airConditioner);
    }

    // 观察者模式
    // 通知空调温度已改变
    public void notifyTemperatureChanged(int temperature) {
        for (AirConditioner airConditioner : airConditioners) {
            airConditioner.setTemperature(temperature);
        }
    }
}
